// pages/info/add.js
var util = require('../../utils/util.js');
var app = getApp();
var today = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 1))).split(' ')[0];
var minday = util.formatTime(new Date()).split(' ')[0];
var maxday = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 62))).split(' ')[0];
Page({
    data: {
        gender: 0,
        date: today,
        start: minday,
        end: maxday,
        time: '请选择时间',
        Surpluss: ['请选择', 1, 2, 3, 4, 5, 6],
        registrationDate: 0,
        number: 0,
        isAgree: false,
        vehicle: ''
    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },
    bindTimeChange: function (e) {
        this.setData({
            time: e.detail.value
        })
    },
    setregistrationDate: function (e) {
        this.setData({registrationDate: e.detail.value})
    },
    setnumber: function (e) {
        this.setData({number: e.detail.value})
    },
    bindAgreeChange: function (e) {
        this.setData({
            isAgree: !!e.detail.value.length
        });
    },
    formSubmit: function (e) {
        var data = e.detail.value;
        var that = this;
        console.log(data);
        if (data.time == '请选择时间') {
            util.isError('请选择出发时间', that);
            return false;
        }

        if (data.surplus == '0') {
            var arr = new Array('', '剩余空位', '乘车人数');
            util.isError('请选择' + arr[data.ordertype], that);
            return false;
        }


        if (!data.isAgree[0]) {
            util.isError('请阅读并同意条款', that);
            return false;
        }
        data.userId = app.globalData.userInfo.id;
        data.departure = that.data.departure;
        data.destination = that.data.destination;
        data.isAgree = data.isAgree[0],
        data.type = that.data.ordertype,
        data.price = that.data.price
        data.startlat = that.data.startlat
        data.startlng = that.data.startlng
        data.endlat = that.data.endlat
        data.endlng = that.data.endlng
        util.req('/wx/wxOrder/add', data, function (data) {
            if (data.code == 1) {
                wx.redirectTo({
                    url: '/pages/info/index?id=' + data.content.id
                });

            } else {
                util.isError(data.msg, that);
                return false;
            }
        })
        util.clearError(that);
    },
    onLoad: function (options) {

        var that = this
        var data={
            startlat:options.startlatitude,
            startlng:options.startlongitude,
            endlat: options.endlatitude,
            endlng: options.endlongitude,
            test:options.test
        }
        util.req("/wx/wxOrder/getPrice",data,function (result) {
            if (result.code == 1){
                console.log(result.content)
                that.setData({
                    name: app.globalData.userInfo.nickName,
                    phone: app.globalData.userInfo.phone,
                    vehicle: app.globalData.userInfo.vehicle,
                    destination: options.end,
                    endlat: options.endlatitude,
                    endlng: options.endlongitude,
                    departure: options.start,
                    startlat:options.startlatitude,
                    startlng:options.startlongitude,
                    price:result.content,
                    ordertype:options.activeIndex
                })
            }else {
                wx.redirectTo({
                    url: '/pages/index/index'
                });
            }
        })
        console.log(that.data)
    }
})